#pragma once
#include <string>
#include<vector>
#include <ctime>
#include <atomic>
#include <fstream>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <boost/algorithm/string.hpp>
namespace NS_UNTIL
{
    const std::string tmp_path = "./tmp/";

    class TimeUntile
    {
    public:
        static std::string GetTimeMess()
        {
            time_t currtime = time(nullptr);
            struct tm *curr = localtime(&currtime);

            char time_buff[128];
            snprintf(time_buff, sizeof(time_buff), "%d-%d-%d %d:%d:%d",
                     curr->tm_year + 1900, curr->tm_mon + 1, curr->tm_mday,
                     curr->tm_hour, curr->tm_min, curr->tm_sec);
            return time_buff;
        }

        static std::string GetTimeMs()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000);
        }
    };

    class PathUntil
    {
    public:
        static std::string AddSuffix(const std::string &path, const std::string &suffix)
        {
            std::string path_name = tmp_path;
            path_name += path;
            path_name += suffix;
            return path_name;
        }
        // 编译的路径工具
        static std::string Src(const std::string &file)
        {
            return AddSuffix(file, ".cpp");
        }
        static std::string Exe(const std::string &file)
        {
            return AddSuffix(file, ".exe");
        }

        static std::string Compile_Error(const std::string &file)
        {
            return AddSuffix(file, ".compile_error");
        }

        // 运行的路径工具
        static std::string Stdin(const std::string &file)
        {
            return AddSuffix(file, ".stdin");
        }
        static std::string Stdout(const std::string &file)
        {
            return AddSuffix(file, ".stdout");
        }
        static std::string stderror(const std::string &file)
        {
            return AddSuffix(file, ".stderror");
        }
    };

    class FileUntil
    {
    public:
        static bool IsExistExe(const std::string &file_path)
        {
            struct stat st;
            if (stat(file_path.c_str(), &st) == 0)
                return true;
            return false;
        }

        // 采用毫秒级时间戳+原子性
        static std::string UniqFileName()
        {
            static std::atomic_int count(0);
            count++;
            std::string file_name = TimeUntile::GetTimeMs();
            file_name += "_" + std::to_string(count);
            return file_name;
        }

        // 123.cpp ->123.cpp
        static bool WriteFile(const std::string &file_path, const std::string &code)
        {
            std::ofstream out(file_path);
            if (!out.is_open())
            {
                return false;
            }
            out.write(code.c_str(), code.size());
            out.close();
            return true;
        }

        // 到指定路径下读取文件,是否保存\n
        static bool ReadFile(const std::string &file_path, std::string *content, bool keep)
        {
            content->clear();
            std::ifstream in(file_path);
            if (!in.is_open())
            {
                return false;
            }
            // getline 默认删除\n
            std::string line;
            while (std::getline(in, line))
            {
                *content += line;
                if (keep)
                    *content += "\n";
            }
            return true;
        }
    };

    class StringUntil
    {
        public:
            static void SplitString(const std::string& src,std::vector<std::string>* target,const std::string& sep)
            {
                //boost库切分字符串
                boost::split((*target),src,boost::is_any_of(sep),boost::token_compress_on);
            }
    };

}
